package mwa.cs.cr.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mwa.cs.dao.AddressDAO;
import mwa.cs.dao.ArDAO;
import mwa.cs.dao.CustomerDAO;
import mwa.cs.database.Database;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Servlet implementation class CustomerStatisticSrvl
 */
public class CS23001Srvl extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private int vnSiteId;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public CS23001Srvl() {
                super();
                // TODO Auto-generated constructor stub
        }

        @SuppressWarnings("unchecked")
        public void processRequest(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/JSON");
                if (request.getParameter("process_type").equals("getDataTable")) {
                        vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
                        String paAccount = request.getParameter("vaAccount");
                        Database db = new Database();
                        ArDAO voARDAO = new ArDAO(db);
                        CustomerDAO voCustomerDAO = new CustomerDAO(db);
                        AddressDAO voAddressDAO = new AddressDAO(db);
                        PrintWriter voOut = response.getWriter();
                        List<Map<String, Object>> voArrayList = null;
                        Map<String, Object> voMap = new HashMap<String, Object>();
                        voMap = voCustomerDAO.getCustomer(vnSiteId,paAccount);
                        String vaAccount = "";
                        String vaName = "";
                        String vaAddress = "";
                        String vaPostCode = "";
                        if(voMap!=null){
                                String paProvince = (String) voMap.get("PROVINCE_CODE");
                                String paDistrict = (String) voMap.get("DISTRICT_CODE");
                                String paCity =  (String) voMap.get("CITY_CODE");
                                if(voMap.get("ACCOUNT") == null){
                                        vaAccount = "";
                                }else{
                                        vaAccount = (String) voMap.get("ACCOUNT");      
                                }
                                if(voMap.get("NAME") == null){
                                        vaName = "";
                                }else{
                                        vaName = (String) voMap.get("NAME");
                                }
                                if(voMap.get("POST_CODE") == null){
                                        vaPostCode = "";
                                }else{
                                        vaPostCode = (String) voMap.get("POST_CODE");
                                } 
                                 
                                 vaAddress = voMap.get("HOME_NUMBER")+" "+voMap.get("ADDRESS")+""
                                                + " "+voAddressDAO.getDistrictName(paProvince, paDistrict)+" "
                                                + ""+voAddressDAO.getCityName(paProvince, paDistrict, paCity)+" "
                                                + ""+voAddressDAO.getProvinceName(paProvince)+" "+vaPostCode;
                                 vaAddress = vaAddress.replace("null", "") ;
                        }
                                JSONObject voObject = new JSONObject();
                                JSONArray voListData = new JSONArray();
                                JSONArray voConsumtion = new JSONArray();
                                JSONArray voDate = new JSONArray();
                                voArrayList = voARDAO.getCustomerStatisticGrap(vnSiteId,paAccount);
//                              System.out.println("voArrayList.size()="+voArrayList.size());
                                if(voArrayList.size()==0){
                                        voDate.add("");
                                        voDate.add("ไม่พบข้อมูลที่ค้นหา");
                                        voDate.add("");
                                        voConsumtion.add("");
                                        voConsumtion.add("");
                                        voConsumtion.add("");
                                        
                                }else if(voArrayList.size()==1){
                                        
                                                voMap = voArrayList.get(0);
                                                voDate.add("");
                                                voDate.add((String) "" + voMap.get("READ_DATE"));
                                                voConsumtion.add("");
                                                voConsumtion.add((String) "" + voMap.get("CONSUMPTION"));
                                                
                                }else{
                                        for (int i = voArrayList.size()-1; i >= 0; i--) {
                                                voMap = voArrayList.get(i);
                                                voDate.add((String) "" + voMap.get("READ_DATE"));
                                                voConsumtion.add((String) "" + voMap.get("CONSUMPTION"));
                                        }       
                                }
                                

                                voListData = voARDAO.getCustomerStatisticDataTable(vnSiteId,paAccount);
//                              System.out.println("voListData="+voListData);
                                voObject.put("aaData", voListData);
                                voObject.put("account", vaAccount);
                                voObject.put("name", vaName);
                                voObject.put("address", vaAddress);
                                voObject.put("consumtion", voConsumtion);
                                voObject.put("date", voDate);
                                
                                db.close();
                                voOut.print(voObject.toJSONString());
                                
                        
                        
                        
                } else if (request.getParameter("process_type").equals(
                                "gotoShowStatistic")) {

                        // get URL_ADDRESS, AGENT_BROWSER
                        // String vaUrlAddress = request.getParameter("url_address");
                        // String vaAgentBrowser = request.getParameter("agent_browser");
                        
                        String vaCustomerAccount = request.getParameter("customer_account");
                        request.setAttribute("vaCustomerAccount", vaCustomerAccount);
                        RequestDispatcher rd = request.getRequestDispatcher("CS23001.jsp");
                        rd.forward(request, response);

                }

        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                processRequest(request, response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                processRequest(request, response);
        }

}